home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-20 / pmpsrc11.zip / KEYS.C < prev    next >
Text File  |  1991-07-30  |  3KB  |  122 lines

  1. /*
  2.     keys.c -- Keystroke handling
  3.  
  4.   Poor Man's Packet (PMP)
  5.   Copyright (c) 1991 by Andrew C. Payne    All Rights Reserved.
  6.  
  7.   Permission to use, copy, modify, and distribute this software and its
  8.   documentation without fee for NON-COMMERCIAL AMATEUR RADIO USE ONLY is hereby
  9.   granted, provided that the above copyright notice appear in all copies.
  10.   The author makes no representations about the suitability of this software
  11.   for any purpose.  It is provided "as is" without express or implied warranty.
  12.  
  13.     May, 1990
  14.     Andrew C. Payne
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <bios.h>
  19. #include <ctype.h>
  20. #include "pmp.h"
  21. #include "keys.h"
  22.  
  23. /* ----- Local Variables ----- */
  24.  
  25. static KEY altcodes[] = {        /* ALT-key scan codes A-Z */
  26.     0x1e00, 0x3000, 0x2e00, 0x2000, 0x1200, 0x2100, 0x2200, 0x2300,
  27.     0x1700, 0x2400, 0x2500, 0x2600, 0x3200, 0x3100, 0x1800, 0x1900,
  28.     0x1000, 0x1300, 0x1f00, 0x1400, 0x1600, 0x2f00, 0x1100, 0x2d00,
  29.     0x1500, 0x2c00 };
  30.  
  31. /* ----- Keystroke buffer handling ----- */
  32. #define KEYBUFLEN 400
  33. static KEY    keybuffer[KEYBUFLEN];        /* keystroke buffer */
  34. static KEY    *keyhead, *keytail;
  35.  
  36. /* Nextkey()
  37.     Returns the next available keypress code.  Returns 0 if no
  38.     keystrokes available.
  39. */
  40. KEY Nextkey(void)
  41. {
  42.     KEY    k;
  43.  
  44.     k = 0;
  45.     if(keyhead != keytail) {
  46.         k = *keyhead++;
  47.         if(keyhead >= keybuffer + KEYBUFLEN)
  48.             keyhead = keybuffer;
  49.     } else if(keypressed())
  50.         k = getkey();
  51.  
  52.     return k;
  53. }
  54.  
  55. /* AddKeystrokes(k)
  56.     Given a string of KEYS, adds keystrokes to the keyboard buffer.
  57.  
  58.     Needs checking for queue overflow.
  59. */
  60. void AddKeystrokes(KEY *s)
  61. {
  62.     while(*s) {
  63.         *keytail++ = *s++;
  64.         if(keytail >= keybuffer + KEYBUFLEN)
  65.             keytail = keybuffer;
  66.     }
  67. }
  68.  
  69. /* InitKeybuffer()
  70.     Initializes things in the key buffer
  71. */
  72. void InitKeybuffer(void)
  73. {
  74.     keytail = keyhead = keybuffer;
  75. }
  76.  
  77. /* ----- String Conversion ----- */
  78.  
  79. /* convertkey(dest, src)
  80.     Given a string in the form:  "testing^M", copies the string to the
  81.     destination, converting all control characters to their IBM PC
  82.     scancode representations.
  83.  
  84.     Returns TRUE if error.
  85. */
  86. int convertkey(KEY *dest, char *src)
  87. {
  88.     int    c;
  89.  
  90.     while(*src && *src != '\n') {
  91.         switch(*src) {
  92.         case '^':        /* control character */
  93.             if(src[1] == '^')
  94.                 c = '^';
  95.             else {
  96.                 c = toupper(src[1]) - '@';
  97.                 if(c < 0 || c > ' ')
  98.                     return TRUE;
  99.             }
  100.             *dest++ = c;
  101.             src += 2;
  102.             break;
  103.         case '~':        /* ALT- keypress */
  104.             if(src[1] == '~') {
  105.                 *dest++ = '~';
  106.             } else {
  107.                 c = toupper(src[1]) - 'A';
  108.                 if(c < 0 || c > 25)
  109.                     return TRUE;        /* error */
  110.                 *dest++ = altcodes[c];
  111.             }
  112.             src += 2;
  113.             break;
  114.         default:
  115.             *dest++ = *src++;
  116.             break;
  117.         }
  118.     }
  119.     *dest = '\0';
  120.     return FALSE;
  121. }
  122.